package edu.gatech.volt2.drdoommgt.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.CascadeType;

import edu.gatech.volt2.drdoommgt.system.DataStore;


@Entity
@Table(name="patients")
public class Patient extends User {
	@OneToOne (cascade= {CascadeType.ALL})
	private PatientInfo info;
	@OneToMany(cascade = {CascadeType.ALL})
	private List<Appointment> appointments;

	
	public Patient(){
		super();
		this.setType(UserType.PATIENT);
	}
	
	public Patient(int id){
		super(id);
		PatientInfo info = new PatientInfo(this);
		this.setInfo(info);
		List<Appointment> appointmentList = Appointment.getListForUser(this);
		this.setAppointments(appointmentList);
		this.setType(UserType.PATIENT);
	}
	
	public Patient(Name name){
		super(name);
		this.setType(UserType.PATIENT);
	}
	
	public Patient(Name patientName, PatientInfo newInfo){
		super(patientName);
		this.setInfo(newInfo);
		this.setType(UserType.PATIENT);
	}
	
	
	public boolean requestAppointment(Appointment a){
		//a.save();
		return true;
	}
	
	public boolean updateAppointment(Appointment a){
		return true;
	}
	
	public void viewMedHistory(){
		// Get list from Patient->info
		// Should this be a "getMedicalHistory" and they we have the Presenter take care of the viewing?
	}


	/**
	 * @param info the info to set
	 */
	public void setInfo(PatientInfo info) {
		this.info = info;
	}


	/**
	 * @return the info
	 */
	public PatientInfo getInfo() {
		return info;
	}


	/**
	 * @param appointments the appointments to set
	 */
	public void setAppointments(List<Appointment> appointments) {
		this.appointments = appointments;
	}


	/**
	 * @return the appointments
	 */
	public List<Appointment> getAppointments() {
		return appointments;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Patient> findByName(Name nameToFind){
		
		Query findByNameQuery=null;
		findByNameQuery = DataStore.getEm().createQuery("from Patient where first = :first_name and last = :last_name and middle = :middle_name");
		findByNameQuery.setParameter("first_name", nameToFind.getFirst());
		findByNameQuery.setParameter("last_name",  nameToFind.getLast());
		findByNameQuery.setParameter("middle_name",  nameToFind.getMiddle());
		List<Patient> returnList = null;
		returnList = findByNameQuery.getResultList();
		return returnList;
		
	}
	
	@SuppressWarnings("unchecked")
	public static List<Patient> findByEmail(String email){
		Query findByEmailQuery=null;
		findByEmailQuery = DataStore.getEm().createQuery("from Patient where email = :email");
		findByEmailQuery.setParameter("email", email);
		List<Patient> returnList = null;
		returnList = findByEmailQuery.getResultList();
		return returnList;
	}

	public List<TreatmentRecord> getTreatmentRecords() {
		return this.getInfo().getMedHistory();
	}

	public void setTreatmentRecords(ArrayList<TreatmentRecord> treatmentRecords) {
		this.getInfo().setMedHistory(treatmentRecords);
	}
	
	public void addTreatmentRecord(TreatmentRecord tr){
		this.getInfo().getMedHistory().add(tr);
	}
	
	public void removeTreatmentRecord(TreatmentRecord tr){
		this.getInfo().getMedHistory().remove(tr);
	}
	
}
